#include "sup_h/httplib.h"
#include <iostream>

using namespace std;

void Hello(const httplib::Request &req, httplib::Response &rsp){
    //设置正文          内容           类型（文本）
    rsp.set_content("Hello world!", "text/plain");
    //设置状态
    rsp.status = 200;
}

void Numbers(const httplib::Request &req, httplib::Response &rsp){
    //req.matches[0]保存的是整体path，之后都是捕捉的数据(\d+)等
    auto num = req.matches[1];
    rsp.set_content(num, "text/plain");
    rsp.status = 200;
}

void Multipart(const httplib::Request &req, httplib::Response &rsp){
    auto ret = req.has_file("file");
    if(ret==false){
        cout << "not file upload" << endl;
        rsp.status = 404;
        return;
    }

    const auto &file = req.get_file_value("file");
    rsp.body.clear();
    rsp.body = file.filename;
    rsp.body += "\n";
    rsp.body += file.content;
    rsp.set_header("Content-Type", "text/plain");
    rsp.status = 200;
}

int main(){

    //实例化一个Server对象用于搭建服务器
    httplib::Server server;
    //注册一个针对/hi的Get请求的处理函数映射关系，当收到/hi请求后调用Hello函数
    server.Get("/hi", Hello);
    //(\d+) -- 正则表达式，和%d作用相同
    server.Get(R"(/numbers/(\d+))", Numbers);

    server.Post("/multipart", Multipart);
    server.listen("0.0.0.0", 9090);

    return 0;
}